<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="异常描述" prop="tagName">
        <el-input
          v-model.trim="queryParams.tagName"
          placeholder="请输入异常描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据表名" prop="tableName">
        <el-input
          v-model.trim="queryParams.tableName"
          placeholder="请输入数据表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="匹配逻辑" prop="matchLogic">
        <el-input
          v-model.trim="queryParams.matchLogic"
          placeholder="请输入匹配逻辑"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否激活" prop="isActive">
        <el-input
          v-model.trim="queryParams.isActive"
          placeholder="请输入是否激活"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="逻辑删除" prop="isDelete">
        <el-input
          v-model.trim="queryParams.isDelete"
          placeholder="请输入逻辑删除"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['abnormal:BiAbnormalRule:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['abnormal:BiAbnormalRule:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['abnormal:BiAbnormalRule:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['abnormal:BiAbnormalRule:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handlecheckAbnormalByIdS"
          >异常检测</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain size="mini" @click="pickEXCELID">选择规则</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!--  @cell-click="tableEdit" -->
    <el-table
      v-loading="loading"
      :data="BiAbnormalRuleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />

      <el-table-column label="异常描述" align="center" prop="tagName">
        <template slot-scope="scope">
          <span v-if="scope.row.tagType == 'define'">{{
            scope.row.tagName
          }}</span>
          <span
            v-if="scope.row.tagType != 'define'"
            style="
              color: rgb(37, 137, 255);
              cursor: pointer;
              text-decoration: underline;
              font-weight: bold;
            "
            @click="gotoApply(scope.row)"
            >{{ scope.row.tagName }}</span
          >
        </template>
      </el-table-column>
      <!-- <el-table-column label="标签类型" align="center" prop="tagType">
        <template slot-scope="scope">
          {{ scope.row.tagType | state1 }}
        </template>
      </el-table-column> -->
      <el-table-column label="数据表名" align="center" prop="tableName" />
      <el-table-column label="匹配逻辑" align="center" prop="matchLogic" />
      <el-table-column label="是否激活" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isActive"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="逻辑删除" align="center" prop="isDelete" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['abnormal:BiAbnormalRule:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['abnormal:BiAbnormalRule:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- <BiRuleSpecifications :row="row" /> -->
    <!-- 添加或修改异常规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数据表名" prop="tableName">
          <el-input
            v-model.trim="form.tableName"
            placeholder="请输入数据表名"
          />
        </el-form-item>
        <el-form-item label="异常描述" prop="tagName">
          <el-input v-model.trim="form.tagName" placeholder="请输入异常描述" />
        </el-form-item>
        <el-form-item label="匹配逻辑" prop="matchLogic">
          <!-- <el-input
            v-model.trim="form.matchLogic"
            placeholder="请输入匹配逻辑"
          /> -->
          <el-select
            v-model="form.matchLogic"
            placeholder="请输入匹配模式"
            clearable
            filterable
          >
            <el-option
              v-for="dict in matchLogicList"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否激活" prop="isActive">
          <el-input v-model.trim="form.isActive" placeholder="请输入是否激活" />
        </el-form-item> -->
        <!-- <el-form-item label="逻辑删除" prop="isDelete">
          <el-input v-model.trim="form.isDelete" placeholder="请输入逻辑删除" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model.trim="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiAbnormalRule,
  getBiAbnormalRule,
  delBiAbnormalRule,
  addBiAbnormalRule,
  updateBiAbnormalRule,
  checkAbnormalByIdS,
} from "@/api/abnormal/BiAbnormalRule";
import BiRuleSpecifications from "@/views/conf/BiRuleSpecifications/BiRuleSpecifications.vue";
export default {
  name: "BiAbnormalRule",
  components: {
    BiRuleSpecifications,
  },
  props: {
    id: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      matchLogicList: ["ANY", "ALL"],
      row: {},
      inputValue: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常规则表格数据
      BiAbnormalRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: null,
        tagName: null,
        tagType: null,
        matchLogic: null,
        isActive: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  filters: {
    state1(string) {
      if (string == "variable") {
        return "变量";
      }
      if (string == "define") {
        return "自定义";
      }
      if (string == "tableRelation") {
        return "表关系";
      }
      // if (int == 2) { return "审批通过" };
    },
  },
  created() {
    this.getList();
  },
  watch: {
    id: {
      handler: function (newnew, oldold) {
        console.log("newnew", newnew);
        if (newnew) {
          this.queryParams.id = newnew;
          // this.queryParams.dateInType = "excel";
          // this.form.id=newnew

          this.getList();
          // this.gettableNameList();
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    pickEXCELID() {
      // if (!this.queryParams.sqlId) {
      //   this.$modal.msgError("这个按钮是对外的");
      //   return;
      // }
      if (this.ids.length != 1) {
        this.$modal.msgError("请只选择一条数据");
        return;
      } else {
        this.$emit("pickEXCELID", this.ids[0]);
      }
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载，请稍候！");
    },
    handlecheckAbnormalByIdS() {
      if (this.ids.length < 1) {
        this.$modal.msgError("请至少选择一条数据");
        return;
      }
      this.openLoading();
      checkAbnormalByIdS(this.ids).then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("检测成功");
          this.getList();
        }
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.isActive === "0" ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '"规则吗？')
        .then(function () {
          return updateBiAbnormalRule({
            id: row.id,
            isActive: row.isActive,
          });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isActive = row.isActive === "0" ? "1" : "0";
        });
    },
    tableEdit(row, column, cell, event) {
      this.row = row;
    },
    /** 查询异常规则列表 */
    getList() {
      this.loading = true;
      listBiAbnormalRule(this.queryParams).then((response) => {
        this.BiAbnormalRuleList = response.rows;
        this.row = response.rows[0];
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableName: null,
        tagName: null,
        tagType: null,
        matchLogic: null,
        isActive: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {};
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加异常规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiAbnormalRule(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改异常规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiAbnormalRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiAbnormalRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除异常规则编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiAbnormalRule(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "abnormal/BiAbnormalRule/export",
        {
          ...this.queryParams,
        },
        `BiAbnormalRule_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
